package com.zkyc.framework.schedulejob.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.zkyc.framework.common.constant.NumberConstant;
import com.zkyc.framework.common.mapper.ClickRateSqlMapper;
import com.zkyc.framework.common.pojo.ClickRate;
import com.zkyc.framework.schedulejob.config.JedisPoolFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import static com.zkyc.framework.common.constant.Constant.*;

/**
 * @author bing.bai,rxc
 */
@Slf4j
@Component
public class SystemClickRateTask {

    @Autowired
    private ClickRateSqlMapper clickRateMapper;

    /**
     * 每四小时同步一次
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void process() {
        Set<String> scan = JedisPoolFactory.scan(REDIS_PREFIX + CLICKRATE);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        // String format = DateUtil.getCurrentTimeStr();
        if (scan != null) {
            for (String key : scan) {
                Integer count = Integer.parseInt(JedisPoolFactory.select(key));
                String[] keySpilt = key.split("_");
                String columnDataIdStr = keySpilt[keySpilt.length - 2];
                Integer columnDataId = Integer.parseInt(columnDataIdStr);
                LambdaQueryWrapper<ClickRate> queryWrapper = getColumnDataClickRateLqw(columnDataId);
                queryWrapper.eq(ClickRate::getClickDate, format)
                        .orderByDesc(ClickRate::getClickRateTotal).last("limit " + NumberConstant.NUMBER_1);
                ClickRate columnDataClickRate = clickRateMapper.selectOne(queryWrapper);
                if (columnDataClickRate == null) {
                    columnDataClickRate = new ClickRate();
                    columnDataClickRate.setClickDate(date);
                    columnDataClickRate.setClickRate(count);
                    columnDataClickRate.setContentId(columnDataId);
                    // 从最近一天的数据获取总点击量
                    LambdaQueryWrapper<ClickRate> columnDataClickRateLqw = getColumnDataClickRateLqw(columnDataId);
                    columnDataClickRateLqw.orderByDesc(ClickRate::getClickDate).last("limit " + NumberConstant.NUMBER_1);
                    ClickRate columnDataClickRateYesterday = clickRateMapper.selectOne(columnDataClickRateLqw);
                    if (columnDataClickRateYesterday == null || columnDataClickRateYesterday.getClickRateTotal() == null) {
                        columnDataClickRate.setClickRateTotal(count);
                    } else {
                        columnDataClickRate.setClickRateTotal(count + columnDataClickRateYesterday.getClickRateTotal());
                    }
                    clickRateMapper.insert(columnDataClickRate);
                } else {
                    columnDataClickRate.setClickRate(columnDataClickRate.getClickRate() + count);
                    columnDataClickRate.setClickRateTotal(columnDataClickRate.getClickRateTotal() + count);
                    clickRateMapper.updateById(columnDataClickRate);
                }
                JedisPoolFactory.delete(REDIS_PREFIX + CLICKRATE + columnDataId + StringPool.UNDERSCORE + NUMBER);
            }
        }
    }

    private LambdaQueryWrapper<ClickRate> getColumnDataClickRateLqw(Integer columnDataId) {
        LambdaQueryWrapper<ClickRate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ClickRate::getContentId, columnDataId);
        return queryWrapper;
    }
}
